<?php
//+----------------------------------------------------------------------
//| ThinkPHP [ WE CAN DO IT JUST THINK IT ]
//+----------------------------------------------------------------------
//| Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
//+----------------------------------------------------------------------
//| Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
//+----------------------------------------------------------------------
//| Author: liu21st <liu21st@gmail.com>
//+----------------------------------------------------------------------
//$Id$
//@last modify 2012-11-26 13:42:40 by mashanling

/**
 +------------------------------------------------------------------------------
 * ThinkPHP 运行时文件 编译后不再加载
 +------------------------------------------------------------------------------
 */
!defined('THINK_PATH') && exit('Access Denied');

//版本信息
define('THINK_VERSION', '3.0');
define('THINK_RELEASE', '20120313');

//系统信息
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
define('GZCOMPRESS'      , function_exists('gzcompress'));
define('IS_CGI'          , 0 === strpos(PHP_SAPI, 'cgi'));//cgi模式
define('IS_WIN'          , false !== strpos(PHP_OS, 'WIN'));//window系统
define('IS_CLI'          , 'cli' == PHP_SAPI);//命令行模式

defined('APP_NAME') or define('APP_NAME', basename(dirname($_SERVER['SCRIPT_FILENAME'])));//项目名称

if(!IS_CLI) {

    if(!defined('_PHP_FILE_')) {//当前文件名

        if(IS_CGI) {//CGI/FASTCGI模式下
            $_temp  = explode('.php', $_SERVER['PHP_SELF']);
            define('_PHP_FILE_',  rtrim(str_replace($_SERVER['HTTP_HOST'], '' , $_temp[0] . '.php'), '/'));
        }
        else {
            define('_PHP_FILE_', $_SERVER['SCRIPT_NAME']);
        }
    }

    if(!defined('__ROOT__')) {//网站URL根目录

        if( strtoupper(APP_NAME) == strtoupper(basename(dirname(_PHP_FILE_))) ) {
            $_root = dirname(dirname(_PHP_FILE_));
        }
        else {
            $_root = dirname(_PHP_FILE_);
        }

        define('__ROOT__', '/' == $_root || '\\' == $_root? '': $_root);
    }
}

//支持的URL模式
define('URL_COMMON',      0);   //普通模式
define('URL_PATHINFO',    1);   //PATHINFO模式
define('URL_REWRITE',     2);   //REWRITE模式
define('URL_COMPAT',      3);   //兼容模式

//路径设置 可在入口文件中重新定义 所有路径常量都必须以/ 结尾
defined('CORE_PATH')        or define('CORE_PATH'       , THINK_PATH . 'Lib/');     //系统核心类库目录
defined('EXTEND_PATH')      or define('EXTEND_PATH'     , THINK_PATH . 'Extend/');    //系统扩展目录
defined('MODE_PATH')        or define('MODE_PATH'       , EXTEND_PATH . 'Mode/');   //模式扩展目录
defined('ENGINE_PATH')      or define('ENGINE_PATH'     , EXTEND_PATH . 'Engine/'); //引擎扩展目录
defined('VENDOR_PATH')      or define('VENDOR_PATH'     , EXTEND_PATH . 'Vendor/'); //第三方类库目录
defined('LIBRARY_PATH')     or define('LIBRARY_PATH'    , EXTEND_PATH . 'Library/');//扩展类库目录

defined('COMMON_PATH')      or define('COMMON_PATH'     , APP_PATH . 'Common/');    //项目公共目录
defined('LIB_PATH')         or define('LIB_PATH'        , APP_PATH . 'Lib/');       //项目类库目录
defined('CONF_PATH')        or define('CONF_PATH'       , APP_PATH . 'Conf/');      //项目配置目录
defined('LANG_PATH')        or define('LANG_PATH'       , APP_PATH . 'Lang/');      //项目语言包目录
defined('TMPL_PATH')        or define('TMPL_PATH'       , APP_PATH . 'Tpl/');       //项目模板目录
defined('HTML_PATH')        or define('HTML_PATH'       , APP_PATH . 'Html/');      //项目静态目录
defined('LOG_PATH')         or define('LOG_PATH'        , RUNTIME_PATH . 'Logs/');  //项目日志目录
defined('TEMP_PATH')        or define('TEMP_PATH'       , RUNTIME_PATH . 'Temp/');  //项目缓存目录
defined('DATA_PATH')        or define('DATA_PATH'       , RUNTIME_PATH . 'Data/');  //项目数据目录
defined('CACHE_PATH')       or define('CACHE_PATH'      , RUNTIME_PATH . 'Cache/'); //项目模板缓存目录

set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);//为了方便导入第三方类库 设置Vendor目录到include_path

/**
 * 创建项目目录结构
 *
 * @last modify 2012-11-30 16:41:07 by mashanling
 *
 * @return void 无返回值
 */
function build_app_dir() {
    !is_dir(APP_PATH) && mk_dir(APP_PATH, 0755);//没有创建项目目录的话自动创建

    if(is_writeable(APP_PATH)) {
        $dirs  = array(
            LIB_PATH,
            RUNTIME_PATH,
            CONF_PATH,
            COMMON_PATH,
            LANG_PATH,
            CACHE_PATH,
            TMPL_PATH,
            TMPL_PATH . C('DEFAULT_THEME') . '/',
            LOG_PATH,
            TEMP_PATH,
            DATA_PATH,
            LIB_PATH . 'Model/',
            LIB_PATH . 'Action/',
            LIB_PATH . 'Behavior/',
            LIB_PATH . 'Widget/',
        );

        foreach ($dirs as $dir){
           !is_dir($dir) && mk_dir($dir, 0755);
        }

        defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', false);//目录安全写入

        if(BUILD_DIR_SECURE) {//自动写入目录安全文件
            defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
            defined('DIR_SECURE_CONTENT')  or define('DIR_SECURE_CONTENT', '');

            $content = DIR_SECURE_CONTENT;
            $a       = explode(',', DIR_SECURE_FILENAME);

            foreach ($a as $filename){

                foreach ($dirs as $dir)
                    file_put_contents($dir . $filename, $content);
            }
        }

        //写入配置文件
        !is_file($filename = CONF_PATH . 'config.php') && file_put_contents($filename, "<?php\nreturn array(\n    //'配置项'=>'配置值'\n);");

        //写入测试Action
        !is_file(LIB_PATH . 'Action/IndexAction.class.php') && build_first_action();
    }
    else{
        exit('项目目录不可写，目录无法自动生成！<br />请使用项目生成器或者手动生成项目目录~');
    }
}//end build_app_dir

/**
 * 创建测试Action
 *
 * @last modify 2012-11-30 16:40:37 by mashanling
 *
 * @return void 无返回值
 */
function build_first_action() {
    $content = file_get_contents(THINK_PATH . 'Tpl/default_index.tpl');
    file_put_contents(LIB_PATH . 'Action/IndexAction.class.php', $content);
}

/**
 * 创建编译缓存
 *
 * @last modify 2012-11-30 16:32:08 by mashanling
 *
 * @return void 无返回值
 */
function build_runtime_cache($append = '') {
    $defs       = get_defined_constants(true);//常量
    $content    =  '';

    if(defined('RUNTIME_DEF_FILE')) { //编译后的常量文件外部引入
        file_put_contents(RUNTIME_DEF_FILE, '<?php ' . array_define($defs['user']));
        $content  .=  'require(\'' . RUNTIME_DEF_FILE . '\');';
    }
    else{
        $content .= array_define($defs['user']);
    }

    $content .= 'set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);';

    $list = array(//读取核心编译文件列表
        THINK_PATH . 'Common/common.php',
        EXTEND_PATH . 'Functions/function.time.php',//时间函数库 by 2012-09-08 09:09:52
        THINK_PATH . 'Common/functions.php', //标准模式函数库 从Think.class.php转至此文件加载 by mashanling on 2012-09-03 10:35:56
        CORE_PATH . 'Core/Think.class.php',
        CORE_PATH . 'Core/Behavior.class.php',
    );

    foreach ($list as $file) {
        $content .= compile($file);
    }

    //系统行为扩展文件统一编译
    $content .= C('APP_TAGS_ON') ? build_tags_cache() : '';

    $alias = include(THINK_PATH . 'Conf/alias.php');
    $content .= 'alias_import(' . var_export($alias,true) . ');';

    //编译框架默认语言包和配置参数
    $content .= $append . "\nL(" . var_export(L(), true) . ");C(" . var_export(C(),true) . ');G(\'loadTime\');Think::start();';
    file_put_contents(RUNTIME_FILE, ('<?php ' . $content));
    file_put_contents(RUNTIME_FILE, php_strip_whitespace(RUNTIME_FILE));
}//end build_runtime_cache

/**
 * 编译系统行为扩展类库
 *
 * @last modify 2012-11-30 16:46:45 by mashanling
 *
 * @return void 无返回值
 */
function build_tags_cache() {
    $tags    = C('extends');
    $content = '';

    foreach ($tags as $tag => $item){

        foreach ($item as $key => $name) {
            $content .= is_int($key) ? compile(CORE_PATH . 'Behavior/' . $name . 'Behavior.class.php') : compile($name);
        }
    }

    return $content;
}

/**
 * 检查缓存目录(Runtime) 如果不存在则自动创建
 *
 * @last modify 2012-11-30 16:05:52 by mashanling
 *
 * @return void 无返回值
 */
function check_runtime() {

    if(!is_dir(RUNTIME_PATH)) {
        mkdir(RUNTIME_PATH);
    }
    elseif(!is_writeable(RUNTIME_PATH)) {
        exit('目录 [ '. RUNTIME_PATH . ' ] 不可写！');
    }

    !is_dir(CACHE_PATH) && mkdir(CACHE_PATH);  //模板缓存目录
    !is_dir(LOG_PATH) && mkdir(LOG_PATH);      //日志目录
    !is_dir(TEMP_PATH) && mkdir(TEMP_PATH);    //数据缓存目录
    !is_dir(DATA_PATH) && mkdir(DATA_PATH);    //数据文件目录
}

/**
 * 加载运行时所需要的文件 并负责自动目录生成
 *
 * @last modify  2012-11-30 16:01:26 by mashanling
 *
 * @return void 无返回值
 */
function load_runtime_file() {
    require(THINK_PATH . 'Common/common.php');//加载系统基础函数库

    $list = array(//读取核心编译文件列表
        EXTEND_PATH . 'Functions/function.time.php',//时间函数库 by 2012-09-08 09:09:52
        THINK_PATH . 'Common/functions.php', //标准模式函数库 从Think.class.php转至此文件加载 by mashanling on 2012-09-03 10:35:56
        CORE_PATH . 'Core/Think.class.php',
        CORE_PATH . 'Core/Behavior.class.php',
    );

    foreach ($list as $file) {//加载模式文件列表
        is_file($file) && require_cache($file);
    }

    alias_import(include(THINK_PATH . 'Conf/alias.php'));//加载系统类库别名定义

    if(!is_dir(LIB_PATH)) {//检查项目目录结构 如果不存在则自动创建
        build_app_dir();//创建项目目录结构
    }
    elseif(!is_dir(CACHE_PATH)){//检查缓存目录
        check_runtime();
    }
    elseif(APP_DEBUG && is_file(RUNTIME_FILE)){//调试模式切换删除编译缓存
        unlink(RUNTIME_FILE);
    }
}//end load_runtime_file

load_runtime_file();//加载运行时所需文件

G('loadTime');//记录加载文件时间

Think::start();//执行入口